/**
 *定义清空下拉列表的全局变量 
 */
var numer = 1;
var numers = 1;

function add(audit) {  
  //创建订单信息
  var typeId=$("select[name='typeId'] option:selected").val();
  var typeName=$("select[name='typeId'] option:selected").text();
  var productId=$("select[name='specIds'] option:selected").val();
  var fullName=$("select[name='specIds'] option:selected").text();
  var salesUserId=$("span[id='userId']").text();
  var salesUserName=$("span[id='userName']").text();
  var dealStatus=$("input[name='dealStatus']").val();
  var customerIdentity=$("span[id='customerIdentity']").text();
  var contractId=$("input[name='contractId']").val();
  var shortToken=$("input[name='shortToken']").val();
  var deptId=$("span[id='deptId']").text();
  var deptName=$("span[id='deptName']").text();
  $.ajax({
    cache : true,
    type : "POST",
    url : ctx + "module/order/saveOrder",
    data :{ 
    	   "typeId" : typeId,
    	   "typeName" : typeName,
    	   "productId" : productId,
    	   "fullName" : fullName,
    	   "salesUserId" : salesUserId,
    	   "salesUserName":salesUserName,
    	   'dealStatus' : dealStatus,
    	   'customerIdentity' : customerIdentity,
    	   'contractId' : contractId,
    	   'shortToken' : shortToken,
    	   'audit' : audit,
    	   'deptId' : deptId,
    	   'deptName' : deptName
    },
    async : false,
    error : function(request) {
      $.modalAlert("系统错误", "error");
    },
    success : function(data) {
		if (data.msg == 1) {
		    parent.layer.msg("新增订单成功,正在刷新数据请稍后……",{icon:1,time: 500,shade: [0.1,'#fff']},function(){
		      $.parentReload();
		    });
		  }else if(data.msg == -2){
			//图片缓存已过期
			$.modalAlert("图片缓存已过期,请重试", "error");
		  }else if(data.msg == -3) {
			//缓存数据异常
			$.modalAlert("缓存数据异常,请重试", "error");
		  }else if(data.msg == -4) {
			//操作异常
			$.modalAlert("未知异常,请重试", "error");
		  }else{
		    $.modalAlert("未知异常", "error");
		  }
		}
  });
}

function selCustomer() {
  var name =  $("input[name='customerCellphone']").val();
  $.ajax({
    cache : true,
    type : "GET",
    dataType: 'json',
    url : ctx + "/module/order/customerList",
    data : {
    	'name' : name
    },
    async : false,
    error : function(request) {
      $.modalAlert("系统错误", "error");
    },
    success : function(data) {
      if (data != null) {
        var item;
        $('#table').show();
        $.each(data, function(index, items){
        item += "<tr><td class='customer-name'>姓名："+items.name+"</td>"
        item +=  "<td class='customer-phone'>手机号码:"+items.cellphone+"</td>"
        item += "<td class='customer-card'>身份证:"+items.identityNum+"</td>"
        item += "<td class='customer-identity-num' name='customnum' style='display:none'>"+items.identityNum+"</td>"
        item += "<td ><button type='button' class='xz'>选择</button></td></tr>";
        });
        $('#table').html(item);                                           
      }
    }
  });
}

/**
 *根据用户名，手机号码查询业务员信息
 */
function selUser() {
  var phonenumber = $("input[name='phonenumber']").val();
  $.ajax({
    cache : true,
    type : "GET",
    dataType: 'json',
    url : ctx + "/module/order/selUser",
    data : { "phonenumber" : phonenumber },
    async : false,
    error : function(request) {
      $.modalAlert("系统错误", "error");
    },
    success : function(data) {
      if (data != null) {
        var item;
        $.each(data, function(index, items){
        item += "<tr><td class='user-name'>姓名:"+items.userName+"</td>"+
        "<td class='user-phone'>手机号码:"+items.phonenumber+"</td>"+
        "<td class='user-cardName'>部门:"+items.dept.deptName+"</td>"+
        "<td class='user-cardId-hidden' style='opacity:0'>"+items.dept.deptId+"</td>"+
        "<td class='user-cardName-hidden' style='opacity:0'>"+items.dept.deptName+"</td>"+
        "<td class='user-userId-hidden' name='userId' style='opacity:0'>"+items.userId+"</td>"+
        "<td class='user-names-hidden' name='userName' style='opacity:0'>"+items.userName+"</td>"+
        //通过按钮获取当前用户信息
         "<td><button type='button' class='button'>选择</button></td></tr>";
        });
        $('#tables').show();
        $('#tables').html(item);
      }
    }
  });  
}

$(function(){
  selcustmom();
  function selcustmom(){
    /**
     * 获取button选择的用户信息
     */
    $('body').on('click','.xz',function(){
      var nameText =  $("#table .customer-name").text();
      var phoneText = $("#table .customer-phone").text();
      var cardText =  $("#table .customer-dept").text();
      //子父级获取数据
      $(".tab-customer-name").text($(this).parent("td").siblings(".customer-name").text());
      $(".tab-customer-phone").text($(this).parent("td").siblings(".customer-phone").text());
      $(".card-customer-id").text($(this).parent("td").siblings(".customer-card").text());
      $(".card-customer-hidden").text($(this).parent("td").siblings(".customer-identity-num").text());
    });
    /**
	 * 获取button选择的业务员信息
	 */
    $('body').on('click','.button',function(){
      var nameText =  $("#table .user-name").text();
      var phoneText = $("#table .user-name").text();
      var cardText =  $("#table .user-card").text();
      //子父级获取数据
      $(".tab-user-name").text($(this).parent("td").siblings(".user-name").text());
      $(".tab-user-userId-hidden").text($(this).parent("td").siblings(".user-userId-hidden").text());
      $(".tab-user-name-hidden").text($(this).parent("td").siblings(".user-names-hidden").text());
      $(".tab-user-phone").text($(this).parent("td").siblings(".user-phone").text());
      $(".tab-user-deptName").text($(this).parent("td").siblings(".user-cardName").text());
      $(".tab-user-cardId-hidden").text($(this).parent("td").siblings(".user-cardId-hidden").text());
      $(".tab-user-deptName-hidden").text($(this).parent("td").siblings(".user-cardName-hidden").text());
    })
  }
})


function parentSelect(){
	var typeId = $("select[name='typeId']").val();
	$.ajax({
        type : "GET",
        url : ctx + "module/order/selectList",
        data : {
        	"typeId": typeId
        },
        success: function(result) {
        	//获取第二个下拉框的对象
        	var spec = document.getElementById('specId');     	       	
        	if(numer==1){
        		numer++;
        	}else{       
        		//每次获取数据前先清空
        		$(spec).empty();        		
        	}      	
        	for(var i=0;i<result.length;i++){//循环遍历数组
        		//将数组的值依次赋给第二个下拉框
        		spec[i] = new Option(result[i].specName,result[i].specId);
			}
        }
    });
}
var obj={}
function childrenSelect(){
	var specId = $("select[name='specId']").val();
	$.ajax({
        type: "GET",
        url: ctx + "module/order/selectProductList",
        data : {
        	"specId": specId
        },
        success: function(result) {
        	//获取第二个下拉框的对象 
        	var spec = document.getElementById('specIds');        	
        	if(numers==1){
        		numers++;
        	}else{
        		//每次获取数据前先清空 
        		$(spec).empty(); 
        	}
        	
        	for(let i=0;i<result.length;i++){//循环遍历数组
        		//将数组的值依次赋给第二个下拉框       		       	
        		obj[result[i].productId] = {};
        		obj[result[i].productId].productPrice = result[i].productPrice;
        		obj[result[i].productId].productTerm = result[i].productTerm;
        		obj[result[i].productId].productId = result[i].productId;
        		obj[result[i].productId].fullName = result[i].fullName;
         		spec[i] = new Option(result[i].fullName,result[i].productId);        	  			         						        		       		
			}
        	$("#specIds").click(function(){
        		var name = $(this).val();       	
        		$("#product-price").text(obj[name].productPrice);         			
        		$("#product-term").text(obj[name].productTerm);
        		$("#productid").text(obj[name].productId).hide(); 
        		$("#fullname").text(obj[name].fullName).hide();	
        	})
        }
    });
}